/*
 * Copyright (C) 2012 xjuraj
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove.jianghongtiao.websearch.service.linkedin;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import org.apache.log4j.Logger;
import sk.lieskove.jianghongtiao.common.enums.searchserviceenum.SearchServiceEnum;
import sk.lieskove.jianghongtiao.common.utils.PropertiesUtils;
import sk.lieskove.jianghongtiao.websearch.service.SearchQuery;
import sk.lieskove.jianghongtiao.websearch.service.SearchService;
import sk.lieskove.jianghongtiao.websearch.persistence.WebSearchResponse;
import com.google.code.linkedinapi.client.LinkedInApiClient;
import com.google.code.linkedinapi.client.LinkedInApiClientFactory;
import com.google.code.linkedinapi.client.oauth.LinkedInAccessToken;
import com.google.code.linkedinapi.client.oauth.LinkedInOAuthService;
import com.google.code.linkedinapi.client.oauth.LinkedInOAuthServiceFactory;
import com.google.code.linkedinapi.client.oauth.LinkedInRequestToken;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import sk.lieskove.jianghongtiao.common.utils.DesktopUtils;

/**
 * 
 * @author xjuraj e-mail: jjurco.sk_gmail.com
 */
public class LinkedInSearch implements Serializable, SearchService {

    private transient Logger log = Logger.getLogger(LinkedInSearch.class);
    private transient static PropertiesUtils pu = new PropertiesUtils(LinkedInSearch.class);

    public static void authentification() {
        String consumerKeyValue = pu.getProperty("linkedin-search-api-key");
        String consumerSecretValue = pu.getProperty("linkedin-search-secret-key");

        LinkedInOAuthService oauthService = LinkedInOAuthServiceFactory.
                getInstance().createLinkedInOAuthService(
                consumerKeyValue, consumerSecretValue);
        LinkedInRequestToken requestToken = oauthService.getOAuthRequestToken();
        final String authUrl = requestToken.getAuthorizationUrl();
        DesktopUtils.openUrlInDefaultBrowser(authUrl);
        System.out.println("Insert Grant code:");
        InputStreamReader converter = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(converter);
        try {
            String oauthVerifier = in.readLine();
            LinkedInAccessToken accessToken = oauthService.getOAuthAccessToken(requestToken, oauthVerifier);
            final LinkedInApiClientFactory factory =
                    LinkedInApiClientFactory.newInstance(consumerKeyValue, consumerSecretValue);
            final LinkedInApiClient client =
                    factory.createLinkedInApiClient(accessToken);
            pu.writeProperty(PropertiesUtils.PROPERTIES_TYPE.STRONG,
                    "search-api-linkedin-access-token", accessToken.getToken());
            pu.writeProperty(PropertiesUtils.PROPERTIES_TYPE.STRONG, 
                    "search-api-linkedin-secret-token", accessToken.getTokenSecret());
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(LinkedInSearch.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public LinkedInApiClient getClient(){
        String consumerKeyValue = pu.getProperty("linkedin-search-api-key");
        String consumerSecretValue = pu.getProperty("linkedin-search-secret-key");
        String token = pu.getProperty("search-api-linkedin-access-token", null);
        String tokenSecret = pu.getProperty("search-api-linkedin-secret-token", null);
        
        if((token == null) || (tokenSecret == null)){
            authentification();
            token = pu.getProperty("search-api-linkedin-access-token", null);
            tokenSecret = pu.getProperty("search-api-linkedin-secret-token", null);
        }
        
        final LinkedInApiClientFactory factory = 
                LinkedInApiClientFactory.newInstance(
                consumerKeyValue, consumerSecretValue);
        final LinkedInApiClient client = factory.createLinkedInApiClient(
                token, tokenSecret);
        return client;
    }

    @Override
    public List<WebSearchResponse> searchAll(SearchQuery query) {

        return null;
    }

    @Override
    public List<WebSearchResponse> searchAll(SearchQuery query, int numberOfResults) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<WebSearchResponse> search(SearchQuery query, SearchServiceEnum[] services) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<WebSearchResponse> search(SearchQuery query, int numberOfResults, SearchServiceEnum[] services) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
